package sojo.com.inverter.service.Impl.device;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import sojo.com.inverter.entity.AlarmDatasVo;
import sojo.com.inverter.entity.InverterCommon;
import sojo.com.inverter.entity.InverterVo;
import sojo.com.inverter.enums.*;
import sojo.com.inverter.kafka.SendMessageKafka;
import sojo.com.inverter.mapper.InverterMapper;
import sojo.com.inverter.modbus.ModbusClient;
import sojo.com.inverter.service.Device;
import sojo.com.inverter.service.ScheduledTaskService;
import sojo.com.inverter.utils.ToolsUtil;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

/**
 * @author cheng_hu
 * @description:
 * @date 2023-02-22 15:33:33
 */
@Slf4j
@Component
@Scope(value = "singleton")
public class DeviceHuaweiInverter implements Device {

    private final static String taskKey = "huaweiInverter";

    @Qualifier("modbusTCPHuaweiInverter")
    @Autowired
    private ModbusClient modbusClient;

    @Autowired
    private ScheduledTaskService scheduledTaskService;

    @Autowired
    private SendMessageKafka sendMessageKafka;

    @Autowired
    private InverterMapper inverterMapper;

    @PostConstruct
    public void init() {
        scheduledTaskService.startTask(taskKey, modbusClient, this);
    }

    @PreDestroy
    public void destroy() {
        scheduledTaskService.stopTask(taskKey);
    }


    @Override
    public void analysisData(InverterVo inverterVo) {
        log.info("接收返回数据 inverterVo :{}", JSONUtil.toJsonStr(inverterVo));

        String deviceId = inverterVo.getSn();

        InverterCommon inverterCommon = new InverterCommon();
        inverterCommon.setTs(ToolsUtil.getTimeStamp(new Date()));
        inverterCommon.setInverterId(deviceId);
        //实时数据类型
        inverterCommon.setDeviceRt("invertering");
        //数据采集模块ID
        inverterCommon.setGateId("1");
        //逆变器整机Id
        inverterCommon.setInverterUnitId("000000000000");
        inverterCommon.setFrequency(inverterVo.getFrequency());
        inverterCommon.setTotalElectric(inverterVo.getTotalElectric());
        inverterCommon.setInverterDayElec(inverterVo.getInverterDayElec());
        //当月发电量  当年发电量
        inverterCommon.setInverterMonthElec(0);
        inverterCommon.setInverterYearElec(0);
        inverterCommon.setActivePower(inverterVo.getActivePower());
        inverterCommon.setReactivePower(inverterVo.getReactivePower());
        inverterCommon.setPowerFactor(inverterVo.getPowerFactor());
        //todo 输入电流电压
        inverterCommon.setInputs("");
        inverterCommon.setVoltageA(inverterVo.getVoltageA());
        inverterCommon.setVoltageB(inverterVo.getVoltageB());
        inverterCommon.setVoltageC(inverterVo.getVoltageC());
        inverterCommon.setCurrentA(inverterVo.getCurrentA());
        inverterCommon.setCurrentB(inverterVo.getCurrentB());
        inverterCommon.setCurrentC(inverterVo.getCurrentC());
        inverterCommon.setVoltageAB(inverterVo.getVoltageAB());
        inverterCommon.setVoltageBC(inverterVo.getVoltageBC());
        inverterCommon.setVoltageAC(inverterVo.getVoltageCA());
        inverterCommon.setInverterEfficiency(inverterVo.getInverterEfficiency());
        inverterCommon.setInverterTem(inverterVo.getInverterTem());
        //
        inverterCommon.setNowElectric(0);
        //控制模式
        inverterCommon.setControlMode("");
        inverterCommon.setRunMode(inverterVo.getRunMode());
        //工作模式
        inverterCommon.setWorkMode("");
        //阵列电压
        inverterCommon.setArrayVoltage(0);
        //阵列电流
        inverterCommon.setArrayCurrent(0);
        //阵列功率
        inverterCommon.setArrayPower(inverterVo.getArrayPower());
        //A B C相功率因数 inverterVo.getPowerFactor()
        inverterCommon.setPowFactorA(0);
        inverterCommon.setPowFactorB(0);
        inverterCommon.setPowFactorC(0);
        //并网视在功率
        inverterCommon.setApparentPower(inverterVo.getApparentPowerMax());
        //日运行时间 1440
        inverterCommon.setDayRunTime(1440);
        //逆变器状态
        inverterCommon.setInverterState(inverterVo.getInverterState());
        inverterCommon.setStartTime(inverterVo.getOpenTime());
        inverterCommon.setShutdownTime(inverterVo.getCloseTime());
        inverterCommon.setRemark("");
        inverterCommon.setCurrentArray(inverterVo.getCurrentArray());
        inverterCommon.setVoltageArray(inverterVo.getVoltageArray());

        log.info("发送kafka inverterCommon：{}", JSONUtil.toJsonStr(inverterCommon));
//        //发送kafka
//        sendMessageKafka.sendMessage(InverterCommon.class, inverterCommon);
//
//        //存时序库
//        try {
//            inverterMapper.createInverterTable("053", deviceId);
//            inverterMapper.insertInverterData(inverterVo);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //告警发送
//        sendAlarm(inverterVo);
    }

    private void sendAlarm(InverterVo inverterVo) {
        String warn_1 = inverterVo.getWarn_1();
        String warn_2 = inverterVo.getWarn_2();
        String warn_3 = inverterVo.getWarn_3();
        String warn_4 = inverterVo.getWarn_4();
        String warn_5 = inverterVo.getWarn_5();

        for (String s : stringToList(warn_1)) {
            AlarmDatasVo alarmDatasVo = new AlarmDatasVo();
            alarmDatasVo.setAlarmTime(String.valueOf(ToolsUtil.getTimeStamp(new Date())));
            alarmDatasVo.setDeviceId(inverterVo.getSn());
            int bit = Integer.parseInt(s);
            int alarmId = AlarmEnum_1.getAlarmId(bit);
            if (alarmId == -1) {
                continue;
            }
            String alarmName = AlarmEnum_1.getAlarmName(bit);
            alarmDatasVo.setAlarmContent(alarmName);
            String alarmLevel = AlarmEnum_1.getAlarmLevel(bit);
            alarmDatasVo.setAlarmLevel(alarmLevel);
            sendMessageKafka.sendMessage(AlarmDatasVo.class, alarmDatasVo);
        }

        for (String s : stringToList(warn_2)) {
            AlarmDatasVo alarmDatasVo = new AlarmDatasVo();
            alarmDatasVo.setAlarmTime(String.valueOf(ToolsUtil.getTimeStamp(new Date())));
            alarmDatasVo.setDeviceId(inverterVo.getSn());
            int bit = Integer.parseInt(s);
            int alarmId = AlarmEnum_2.getAlarmId(bit);
            if (alarmId == -1) {
                continue;
            }
            String alarmName = AlarmEnum_2.getAlarmName(bit);
            alarmDatasVo.setAlarmContent(alarmName);
            String alarmLevel = AlarmEnum_2.getAlarmLevel(bit);
            alarmDatasVo.setAlarmLevel(alarmLevel);
            sendMessageKafka.sendMessage(AlarmDatasVo.class, alarmDatasVo);
        }

        for (String s : stringToList(warn_3)) {
            AlarmDatasVo alarmDatasVo = new AlarmDatasVo();
            alarmDatasVo.setAlarmTime(String.valueOf(ToolsUtil.getTimeStamp(new Date())));
            alarmDatasVo.setDeviceId(inverterVo.getSn());
            int bit = Integer.parseInt(s);
            int alarmId = AlarmEnum_3.getAlarmId(bit);
            if (alarmId == -1) {
                continue;
            }
            String alarmName = AlarmEnum_3.getAlarmName(bit);
            alarmDatasVo.setAlarmContent(alarmName);
            String alarmLevel = AlarmEnum_3.getAlarmLevel(bit);
            alarmDatasVo.setAlarmLevel(alarmLevel);
            sendMessageKafka.sendMessage(AlarmDatasVo.class, alarmDatasVo);
        }

        for (String s : stringToList(warn_4)) {
            AlarmDatasVo alarmDatasVo = new AlarmDatasVo();
            alarmDatasVo.setAlarmTime(String.valueOf(ToolsUtil.getTimeStamp(new Date())));
            alarmDatasVo.setDeviceId(inverterVo.getSn());
            int bit = Integer.parseInt(s);
            int alarmId = AlarmEnum_4.getAlarmId(bit);
            if (alarmId == -1) {
                continue;
            }
            String alarmName = AlarmEnum_4.getAlarmName(bit);
            alarmDatasVo.setAlarmContent(alarmName);
            String alarmLevel = AlarmEnum_4.getAlarmLevel(bit);
            alarmDatasVo.setAlarmLevel(alarmLevel);
            sendMessageKafka.sendMessage(AlarmDatasVo.class, alarmDatasVo);
        }

        for (String s : stringToList(warn_5)) {
            AlarmDatasVo alarmDatasVo = new AlarmDatasVo();
            alarmDatasVo.setAlarmTime(String.valueOf(ToolsUtil.getTimeStamp(new Date())));
            alarmDatasVo.setDeviceId(inverterVo.getSn());
            int bit = Integer.parseInt(s);
            int alarmId = AlarmEnum_5.getAlarmId(bit);
            if (alarmId == -1) {
                continue;
            }
            String alarmName = AlarmEnum_5.getAlarmName(bit);
            alarmDatasVo.setAlarmContent(alarmName);
            String alarmLevel = AlarmEnum_5.getAlarmLevel(bit);
            alarmDatasVo.setAlarmLevel(alarmLevel);
            sendMessageKafka.sendMessage(AlarmDatasVo.class, alarmDatasVo);
        }

    }

    public String[] stringToList(String str) {
        return str.split(",");
    }
}
